﻿using Xejen.Languages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace Xejen.Languages.Test
{
    public partial class MainForm : Form
    {
        private readonly ILanguageManager _languageManager;
        public MainForm(ILanguageManager languageManager)
        {
            _languageManager = languageManager;

            InitializeComponent();

            languageManager.LanguageChanged += (s, e) => Localize();
        }

        private void Localize()
        {
            ButtonChangeLanguage.Text = _languageManager.CurrentLanguage["Buttons.ButtonMount"];
            FrmCallRepairText.Text = _languageManager.CurrentLanguage["Labels.mazhiyuan"];

            //测试多级
            if (PauseButton.Text == _languageManager.CurrentLanguage["DataSyncRecordsControl.PauseButton.Pause"])
            {
                PauseButton.Text = _languageManager.CurrentLanguage["DataSyncRecordsControl.PauseButton.Resume"];
            }
            else
            {
                PauseButton.Text = _languageManager.CurrentLanguage["DataSyncRecordsControl.PauseButton.Pause"];
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var currentLanguage = _languageManager.CurrentLanguage;

            LanguageComboBox.DataSource = _languageManager.GetLanguages().ToList();
            LanguageComboBox.DisplayMember = "Value";
            LanguageComboBox.ValueMember = "Key";

            LanguageComboBox.SelectedValue = currentLanguage.LanguageCode;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _languageManager.ChangeLanguage("en-US");
            //_languageManager.ChangeLanguage("zh-CN");
        }

        private void LanguageComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LanguageComboBox.SelectedIndex == -1)
            {
                return;
            }

            var code = ((KeyValuePair<string, string>)LanguageComboBox.SelectedItem).Key;

            _languageManager.ChangeLanguage(code);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下");
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            var data = @"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
              <soap:Header>
                <LoginSoapHeader xmlns=""http://tempuri.org/"">
                  <EquUserID>admin</EquUserID>
                  <EquPassword>admin123</EquPassword>
                </LoginSoapHeader>
              </soap:Header>
              <soap:Body>
                <HelloWorld xmlns=""http://tempuri.org/"">
                  <body>
                    <EquipmentCode>${CCD_Ca_ofRetain1}</EquipmentCode>
                  </body>
                </HelloWorld>
              </soap:Body>
            </soap:Envelope>";

            //var data = @"
            //    {
            //        ""name"": ""John"",
            //        ""age"": 30,
            //        ""isStudent"": false,
            //        ""address"": null,
            //        ""courses"": [""Math"", ""Science""]
            //    }
            //";


            //var data = @"{""TaskId"":4524, ""Address"":{""HttpConfig"":{""ContentType"":""text/xml"", ""Encoding"":{""BodyName"":""utf-8"", ""EncodingName"":""Unicode (UTF-8)"", ""HeaderName"":""utf-8"", ""WebName"":""utf-8"", ""WindowsCodePage"":1200, ""IsBrowserDisplay"":true, ""IsBrowserSave"":true, ""IsMailNewsDisplay"":true, ""IsMailNewsSave"":true, ""IsSingleByte"":false, ""EncoderFallback"":{""DefaultString"":""�"", ""MaxCharCount"":1}, ""DecoderFallback"":{""DefaultString"":""�"", ""MaxCharCount"":1}, ""IsReadOnly"":true, ""CodePage"":65001}, ""EncodingName"":""UTF-8"", ""UserName"":""SUP_JCC01_CL01"", ""Password"":""CA@JCmesCL01"", ""Enable"":true, ""Timeout"":50000, ""HeartbeatConfig"":{""Enable"":true, ""Interval"":4567}, ""JsonSerializerOptions"":{""Converters"":[], ""TypeInfoResolver"":{""Modifiers"":[]}, ""TypeInfoResolverChain"":[], ""AllowTrailingCommas"":false, ""DefaultBufferSize"":16384, ""Encoder"":{""MaxOutputCharactersPerInputCharacter"":6}, ""IgnoreNullValues"":false, ""DefaultIgnoreCondition"":""Never"", ""NumberHandling"":""Strict"", ""PreferredObjectCreationHandling"":""Replace"", ""IgnoreReadOnlyProperties"":false, ""IgnoreReadOnlyFields"":false, ""IncludeFields"":false, ""MaxDepth"":0, ""PropertyNamingPolicy"":""System.Text.Json.JsonCamelCaseNamingPolicy"", ""PropertyNameCaseInsensitive"":true, ""ReadCommentHandling"":""Disallow"", ""UnknownTypeHandling"":""JsonElement"", ""UnmappedMemberHandling"":""Skip"", ""WriteIndented"":false, ""IsReadOnly"":true}, ""LogVerbosity"":""Normal""}, ""RequestAddress"":{""Value"":{""RequestUri"":""http://192.168.3.164:112/WebService1.asmx?op=HelloWorld""}, ""HasValue"":true}, ""Headers"":[""Username"",""Password""]}, ""TimeOut"":50000, ""Response"":{""Code"":200, ""IsSuccess"":true, ""ReturnData"":""<?xml version=\""1.0\"" encoding=\""utf-8\""?><soap:Envelope xmlns:soap=\""http://schemas.xmlsoap.org/soap/envelope/\"" xmlns:xsi=\""http://www.w3.org/2001/XMLSchema-instance\"" xmlns:xsd=\""http://www.w3.org/2001/XMLSchema\""><soap:Body><HelloWorldResponse xmlns=\""http://tempuri.org/\""><HelloWorldResult><Code>-1</Code><Msg>用户验证通过！</Msg></HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>""}}";


            //var data = "土木工程魂牵梦萦32大声道";

            textBox1.Text = data;

            Xejen.Editor.DataVisualizeForm form = new Editor.DataVisualizeForm(data, readWriteType: Editor.ReadWriteType.Write);
            if (form.ShowDialog() == DialogResult.OK)
            {
                var value = await form.GetDataAsync();

                textBox1.Text = value;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            //预初始化：事先在主界面实例化一次，后面实际再打开时就很快
            //new WebView2();
            Xejen.Editor.DataVisualizeForm form = new Editor.DataVisualizeForm(string.Empty);
            form.Preload();
        }
    }
}
